package com.shop.controller.admin;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.jf.core.BaseController;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.shop.bean.DpglBean;
import com.shop.bean.NzcpdlBean;
import com.shop.utils.StringUtils;

public class DpglController extends BaseController{
	/**
	 * 获取所有的店铺
	 * */
	public void getAllDpgl(){
		String select = "select *";
		String sqlExceptSelect = "from tbldpgl order by dpcode desc";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<DpglBean> dpgls = DpglBean.dpglDao.paginate(page, 55, select,sqlExceptSelect);
		setAttr("TotalRow", dpgls.getTotalRow());
		setAttr("PageNumber",dpgls.getPageNumber());
		setAttr("dpglslist", dpgls.getList());
		
		render(WEB_INF_URL+"admin/files/listdpgls.jsp");
	}
	
	/**
	 * 跳转进入店铺添加页面
	 * */
	public void gotoAddPage(){
		String timecode = StringUtils.getTimeCode();
		setAttr("dpcode",timecode);
		String sql_ = StringUtils.getSql("sql.find.findCpdlList");//获取大类
		List<NzcpdlBean> list = NzcpdlBean.cpdlDao.find(sql_);
		setAttr("cpdlList",list);
		render(WEB_INF_URL+"admin/files/adddpgl.jsp");
	}
	
	/**
	 * 判断店铺名称是否存在
	 * */
	public void valDpName(){
		String dpname = getPara("dpname");
		String sql_ = StringUtils.getSql("sql.find.findDpglByName", dpname);
		DpglBean dpgl = DpglBean.dpglDao.findFirst(sql_);
		if(dpgl==null){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 添加一个店铺
	 * */
	public void adddpglOption(){
		String dptime = StringUtils.getFormatTime(new Date());
		String dpcode = getPara("dpcodeVal");
		String dpname = getPara("dpname");
		String dpjyfw = getPara("dpjyfw");
		String dpdlstyle = getPara("cpdlSel");
		String dpfuzeren = getPara("dpfzr");
		String fzrtel = getPara("dpfzrtel");
		String dpsjjs = getPara("dpgljs");
		String fukuanstyle = getPara("dgglfksm");
		String peisong = getPara("pssm");
		String jjfenduihuan = getPara("jfdhsm");
	    DpglBean dpglBean = new DpglBean();
	    boolean bool = dpglBean.set("dpcode", dpcode).set("dpname", dpname).set("dpjyfw", dpjyfw)
	    .set("dpdlstyle", dpdlstyle).set("dpfuzeren", dpfuzeren).set("fzrtel", fzrtel)
	    .set("dpsjjs", dpsjjs).set("fukuanstyle", fukuanstyle).set("peisong", peisong)
	    .set("jjfenduihuan", jjfenduihuan).set("dptime",dptime)
	    .set("lasttime",dptime).set("dpstate",1).save();
	    if(bool){
	    	redirect("getAllDpgl");
	    }else{
	    	
	    }
	}
	
	/**
	 * 查看店铺详细信息
	 * */
	public void goToDetail(){
		String dpcode = getPara("dpcode");
		String sql_ = StringUtils.getSql("sql.find.findDpglByCode", dpcode);
		DpglBean dpglBean = DpglBean.dpglDao.findFirst(sql_);
		System.out.println(dpglBean.getStr("dpname"));
		setAttr("dpgl",dpglBean);
		render(WEB_INF_URL+"admin/files/dpgldetail.jsp");
	}
	
	/**
	 * 激活选择的店铺
	 * */
	public void jhDpglList(){
		final String dpcodeList = getPara("dpcodeList");
		boolean bool = Db.tx(new IAtom(){
			@Override
			public boolean run() throws SQLException {
				boolean result = true;
				String[] dpcodeArr = dpcodeList.split("@");
				for(String dpcode: dpcodeArr){
					result = DpglBean.dpglDao.findById(dpcode).set("dpstate", 1).update();
					if(!result){
						break;
					}
				}
				return result;
			}
		});
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	} 
	
	/**
	 * 假删除选择的店铺,就是把其状态变为不可用
	 * */
	public void delDpglList(){
		final String dpcodeList = getPara("dpcodeList");
		boolean bool = Db.tx(new IAtom(){
			@Override
			public boolean run() throws SQLException {
				boolean result = true;
				String[] dpcodeArr = dpcodeList.split("@");
				for(String dpcode: dpcodeArr){
					result = DpglBean.dpglDao.findById(dpcode).set("dpstate", 0).update();
					if(!result){
						break;
					}
				}
				return result;
			}
		});
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 进入店铺信息编辑页面
	 * */
	public void goToEdit(){
		String dpcode = getPara("dpcode");
		DpglBean dpgl = DpglBean.dpglDao.findById(dpcode);
		setAttr("dpgl",dpgl);
		String sql_ = StringUtils.getSql("sql.find.findCpdlList");
		List<NzcpdlBean> list = NzcpdlBean.cpdlDao.find(sql_);
		setAttr("cpdlList",list);
		render(WEB_INF_URL+"admin/files/editdpgl.jsp");
	}
	
	/**
	 * 编辑店铺信息
	 * */
	public void editdpglOption(){
		String dptime = StringUtils.getFormatTime(new Date());
		String dpcode = getPara("dpcodeVal");
		//String dpname = getPara("dpname");
		String dpjyfw = getPara("dpjyfw");
		String dpdlstyle = getPara("cpdlSel");
		String dpfuzeren = getPara("dpfzr");
		String fzrtel = getPara("dpfzrtel");
		String dpsjjs = getPara("dpgljs");
		String fukuanstyle = getPara("dgglfksm");
		String peisong = getPara("pssm");
		String jjfenduihuan = getPara("jfdhsm");
	    boolean bool = DpglBean.dpglDao.findById(dpcode).set("dpjyfw", dpjyfw)
	    .set("dpdlstyle", dpdlstyle).set("dpfuzeren", dpfuzeren).set("fzrtel", fzrtel)
	    .set("dpsjjs", dpsjjs).set("fukuanstyle", fukuanstyle).set("peisong", peisong)
	    .set("jjfenduihuan", jjfenduihuan).set("lasttime",dptime).update();
	    if(bool){
	    	redirect("getAllDpgl");
	    }else{
	    	
	    }
	}
}
